package net.huazai.exception;

import lombok.extern.slf4j.Slf4j;
import net.huazai.utils.ApiResult;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class BizExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ApiResult handle(Exception e){
        // 判断是否是业务异常还是非业务异常
        if (e instanceof BizException) {
            BizException bizException = (BizException) e;
            log.error("[这里是业务异常信息]状态码：{}，具体内容如下：{}"
                    , bizException.getCode(), bizException.getMessage());
            return ApiResult.doError(bizException.getCode(), bizException.getMessage());
        } else {
            log.info("[这里是系统异常信息，具体内容如下：]{}", e.getMessage());
            return ApiResult.doError(HttpStatus.INTERNAL_SERVER_ERROR.value()
                    , "这里是系统异常" + e);
        }
    }
}
